<template>
  <div>
    <Card>
      <p slot="title" class="card-title">
        <Icon type="home"></Icon>
        <span>企业管理</span>
      </p>
      <div>
        <template>
          <Row>
            <Col span="15">
              <Col span="3" style="width:auto;">
                <Button  v-if="tableData.data.length<=0&&managerFlag " type="info" @click="enterprise={};editModal=true;">
                  <Icon type="md-add" ></Icon>&nbsp;新建企业
                </Button>
              </Col>
              <Col span="3" style="width:auto;">
                <Button :disabled="setting.loading" type="success" @click="getList()">
                  <Icon type="md-refresh"></Icon>&nbsp;刷新数据
                </Button>
              </Col>
            </Col>
          </Row>
          <Table
            ref="table"
            class="margin-bottom-10"
            :columns="columns"
            :loading="setting.loading"
            border 
            :data="tableData.data"
            size="large"
            style="margin-top:10px;"
          >
            <template slot-scope="{ row,index  }" slot="action">
              <Tooltip content="编辑" :transfer="true">
                <Button
                  size="small"
                  icon="ios-create-outline"
                  :disabled="row.delete"
                  type="default"
                  shape="circle"
                  border
                  @click="enterprise=Object.assign({},row);editModal=true;"
                ></Button>
              </Tooltip>
              <Tooltip content="停用" :transfer="true" v-if="false">
                <i-switch
                  size="small"
                  :value="row.delete"
                  v-model="row.delete"
                  :disabled="row.delete"
                  @on-change="deleteUser(row)"
                ></i-switch>
              </Tooltip>
            </template>
          </Table>
          <Page
            :total="tableData.total"
            size="small"
            show-total
            show-elevator
            class="tr"
            @on-change="pageChange"
            :current.sync="tableData.current"
            :page-size="tableData.size"
          ></Page>
        </template>
      </div>
    </Card>
       <Edit v-if="editModal" :enterprise="enterprise" @cancel="onModalCancel"/>

  </div>
</template>
<script>
import Edit from "@/view/system/enterprise/components/edit.vue"
import { checkRole, allRoles } from "@/libs/util";


export default {
  data() {
    return {
      setting: {
        loading: false
      },
      search: {},
      enterprise:{},
      editModal:false,
      adminFlag:false,
      managerFlag:false,
      tableData: this.initTableData(),
      columns: [
        { title: "企业名称", key: "name" },
        { title: "企业代码", key: "id" },
        { title: "操作", slot: "action" },
      ]
    };
  },
  components: {
      Edit
  },
  created() {
    this.managerFlag = checkRole(allRoles.manager);
    this.adminFlag = checkRole(allRoles.admin);
    this.getList();
  },
  methods: {
    initTableData() {
      return {
        data: [],
        current: 1,
        size: 10
      };
    },
    getList() {
      var url = "/enterprise/list/{pageNo}";
      var ps = {};
      this.setting.loading=true;
      ps = Object.assign(ps, this.search);

      this.post(url, ps, { pageNo: this.tableData.current }).then(res => {
                this.setting.loading=false;

        if (this.checkIsSsuccess(res)) {
          this.tableData.data = res.data.content;
          this.tableData.total = res.data.totalElements;
          this.tableData.totalPages = res.data.totalPages;
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 0,
            closable: true
          });
        }
      });
    },
    deleteUser(data) {
      this.$Modal.confirm({
        title: "提示",
        content: "确定停用企业(停用后无法恢复)?",
        onOk: () => {
          var url = "/user/delete/{id}";
          var ps = {};

          this.post(url, ps, { id: data.id }).then(res => {
            this.$Modal.remove();
            if (this.checkIsSsuccess(res)) {
              this.$Message.success({
                content: "停用成功",
                duration: 0,
                closable: true,
                onClose: () => this.getList()
              });
            } else {
              this.$Message.error({
                content: res["msg"],
                duration: 0,
                closable: true,
                onClose: () => this.getList()
              });
            }
          });
        },
        onCancel: () => {
          data.delete = false;
        },
      });
    },
    pageChange() {
      this.getList();
    },
    onModalCancel(type,reload) {
        if('edit'==type){
            this.editModal=false;
        }
        if(reload){
            this.getList();
        }
    },
  }
};
</script>